﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GeoWeb.Common;

namespace GeoWeb.Demo.WorkDataConfig
{
    /// <summary>
    /// WorkDay 的摘要说明
    /// </summary>
    public class WorkDay : IHttpHandler
    {
        /// <summary>
        /// 数据库操作类
        /// </summary>
        GeoWeb.Demo.WorkDataConfig.WorkDataConfigDal WorkDayDal = new WorkDataConfigDal();

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Params["action"];
            string sMessage = string.Empty;
            switch (action)
            {
                case "load":
                    sMessage = GetJsonByYearAndMonth(context);
                    break;
                case "save":
                    sMessage = SetFreeDay(context);
                    break;
                case "getwcount":
                    sMessage = GetWorkdayCount(context);
                    break;
                default:
                    break;
            }

            context.Response.Clear();
            context.Response.Write(sMessage);
            context.Response.End();
        }

        #region 获取指定年指定月的非工作日JSON数据集
        /// <summary>
        /// 获取指定年指定月的非工作日JSON数据集
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string GetJsonByYearAndMonth(HttpContext context)
        {
            string Result = "{\"state\":false}";

            int year = 0, month = 0;
            int.TryParse(context.Request.Params["wyear"], out year);
            int.TryParse(context.Request.Params["wmonth"], out month);

            if (year == 0 || month == 0)
                return Result;

            if (!WorkDayDal.InitMonthByYear(year, month))
                return Result;

            List<GeoWorkDay> list = WorkDayDal.GetListDateByYearAndMonth(year, month);
            Result = "{\"state\":true";
            if (list.Count > 0)
            {
                JsonHelper json = new JsonHelper();
                Result += ",\"rows\":" + json.ObjectToJSON(list);
            }
            else
            {
                Result += ",\"rows\":[]";
            }
            Result += "}";
            return Result;
        }
        #endregion

        #region 设置非工作日或工作日的方法
        /// <summary>
        /// 设置非工作日或工作日的方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string SetFreeDay(HttpContext context)
        {
            string Result = "{\"state\":false}";
            int year = 0, month = 0, dayType = 0;
            string Days = context.Request.Params["wdays"];
            int.TryParse(context.Request.Params["wyear"], out year);
            int.TryParse(context.Request.Params["wmonth"], out month);
            int.TryParse(context.Request.Params["daystate"], out dayType);

            if (string.IsNullOrEmpty(Days))
                return Result;

            if (year == 0 || month == 0)
                return Result;

            Days = Days.TrimEnd(',');

            string[] str_Arry = Days.Split(',');

            int[] int_Arry = new int[str_Arry.Length];

            for (int i = 0; i < str_Arry.Length; i++)
            {
                int_Arry[i] = Convert.ToInt32(str_Arry[i]);
            }

            bool bState = WorkDayDal.SetDaysState(year, month, dayType, int_Arry);

            if (bState)
            {
                Result = "{\"state\":true}";
            }
            return Result;
        }
        #endregion

        #region 根据指定时间段获取时间段内的工作日及非工作日天数
        /// <summary>
        /// 根据指定时间段获取时间段内的工作日及非工作日天数
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string GetWorkdayCount(HttpContext context)
        {
            string Result = "{\"state\":false}";
            //起始日期
            DateTime startDate = DateTime.MinValue;
            //结束日期
            DateTime endDate = DateTime.MinValue;

            DateTime.TryParse(context.Request.Params["sdate"], out startDate);
            DateTime.TryParse(context.Request.Params["edate"], out endDate);

            if (startDate == DateTime.MinValue || endDate == DateTime.MinValue)
                return Result;
            //获取工作日天数及非工作日天数
            List<int> temp = WorkDayDal.GetWorkDayCount(startDate, endDate);

            if (temp.Count > 0)
            {
                Result = "{\"state\":true,\"workday\":" + temp[0].ToString() + ",\"freeday\":" + temp[1].ToString() + "}";
            }

            return Result;
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}